package nc.impl.hrpbm.budgetmake.budgetmake;

import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ObjectUtils;

import com.ufida.report.anareport.FreeReportContextKey;
import com.ufida.report.anareport.base.BaseQueryCondition;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.hrpbm.budgetmake.IBudgetMakeService;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.pub.hrpbm.util.HrpbmReportUtils;
import nc.pub.smart.context.SmartContext;
import nc.pub.smart.data.DataSet;
import nc.pub.smart.data.DbTable;
import nc.vo.pub.BusinessException;
import nc.vo.pub.JavaType;

import com.ufida.dataset.IContext;

import nc.pub.smart.data.DataSet;
import nc.pub.smart.metadata.Field;
import nc.pub.smart.metadata.MetaData;
import nc.vo.pub.BusinessException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class BudgetMakeServiceImpl implements IBudgetMakeService{
	public BaseDAO getBaseDAO() {
		return baseDAO == null ? new BaseDAO() : baseDAO;
	}
	private BaseDAO baseDAO;
	HrpbmReportUtils Utils = new HrpbmReportUtils();
	String[] fields = new String[] {"amount_firstup", "inoutbusiclassname",
			"pk_budgetdept","deptname","pk_projectclass",
			"type_name","pk_updept","pk_fundsource",
			"billno","pk_budgetmake","pk_project","inoutbusiclasscode",
			"f1project_name","f2project_name","f3project_name",
			
			"amount_oneup","amount_onedown","amount_twoup","amount_twodown",
			"oneup_01","onedown_01","twoup_01","twodown_01",
			"oneup_02","onedown_02","twoup_02","twodown_02",
			"oneup_03","onedown_03","twoup_03","twodown_03",
			"oneup_04","onedown_04","twoup_04","twodown_04",
			"oneup_05","onedown_05","twoup_05","twodown_05"};
	String[] fieldTypes =new String[] {"String","String","String","String",
			"String","String","String","String",
			"String","String","String","String",
			"String","String","String",
			"Double","Double" ,"Double",
			"Double" ,"Double" ,"Double" ,"Double" ,
			"Double" ,"Double" ,"Double" ,"Double" ,
			"Double" ,"Double" ,"Double" ,"Double" ,
			"Double" ,"Double" ,"Double" ,"Double" ,
			"Double" ,"Double" ,"Double" ,"Double" ,"Double"};
	@SuppressWarnings("unchecked")
	@Override
	public  DataSet process(SmartContext context) throws BusinessException {
		

	        //获取查询条件
		
		HrpbmReportUtils.handelContext(context);
		
	
		
//	        BaseQueryCondition basecon = (BaseQueryCondition) context
//	                .getAttribute(FreeReportContextKey.KEY_IQUERYCONDITION);
	        //初始化元数据
	        MetaData meta = HrpbmReportUtils.getMetaDatab(fieldTypes, fields);

	 
	        //获取业务数据，根据自己的业务，自己设计结构，拿到一个二维数组。在这仅写了简单的构建二维数组数据。
	        List businessData =getBusinessData(context);
	 
	        return HrpbmReportUtils.buildDataSetobj(meta, businessData, fields);
		

	}
	private List getBusinessData(SmartContext context) throws DAOException {
		
		
		String pk_org = (String) context.getAttribute("pk_org");
		String budgetdept = (String) context.getAttribute("pk_budgetdept");
		String inoutlevel = (String) context.getAttribute("level");
		String pk_inoutclass = (String) context.getAttribute("pk_inoutclass");
		String nyear = (String) context.getAttribute("nyear");
		
		String sql = "SELECT inoutbusiclass.code inoutbusiclasscode,\r\n" + 
				"         inoutbusiclass.name inoutbusiclassname,\r\n" + 
			
				"         inoutbusiclass.pk_parent inoutbusiclassparent,\r\n" + 
				"         detail.pk_inoutclass pk_inoutclass,\r\n" + 
				"         make.pk_budgetdept pk_budgetdept,\r\n" + 
				"         budgetdept.name deptname,\r\n" + 
				//"         projectclass.pk_projectclass pk_projectclass,\r\n" + 
				"         projectclass.eps_name type_name,\r\n" + 
				"         make.pk_updept pk_updept,\r\n" + 
				"         detail.pk_fundsource pk_fundsource,\r\n" +
				"         fundsource.code sourcecode,"+
				"         make.billno billno,\r\n" + 
				"         make.pk_budgetmake pk_budgetmake,\r\n" + 
				"         detail.pk_project pk_project,\r\n" + 
				"         CASE   \r\n" + 
				"       WHEN project.def1='1' THEN project.project_name  \r\n" + 
				"       WHEN f2project.def1='1' THEN f2project.project_name\r\n" + 
				"			 WHEN f3project.def1='1' THEN f3project.project_name 	 \r\n" + 
				"       ELSE null  \r\n" + 
				"       END AS f1project_name,\r\n" + 
				"			 \r\n" + 
				"CASE   \r\n" + 
				"       WHEN project.def1='2' THEN project.project_name  \r\n" + 
				"       WHEN f2project.def1='2' THEN f2project.project_name\r\n" + 
				"			 WHEN f3project.def1='2' THEN f3project.project_name 		 \r\n" + 
				"       ELSE null \r\n" + 
				"       END AS f2project_name,\r\n" + 
				"CASE   \r\n" + 
				"       WHEN project.def1='3' THEN project.project_name  \r\n" + 
				"       WHEN f2project.def1='3' THEN project.project_name\r\n" + 
				"			 WHEN f3project.def1='3' THEN project.project_name 	 \r\n" + 
				"       ELSE null   \r\n" + 
				"       END AS f3project_name," + 
				"         make.totalamount totalamount,\r\n" + 
				"         detail.amount_firstup amount_firstup,\r\n" + 
				"         detail.amount_firstdown amount_firstdown,\r\n" + 
				"         detail.amount_secondup amount_secondup,\r\n" + 
				"         detail.amount_seconddown amount_seconddown,\r\n" + 
				"         inoutbusiclass.def1 classlevel \r\n" + 
				"FROM hrpbm_budgetmake make\r\n" + 
				"LEFT JOIN hrpbm_budgetmakedetail detail\r\n" + 
				"    ON make.pk_budgetmake=detail.pk_budgetmake\r\n" + 
				"LEFT JOIN hrpbm_budgetdept budgetdept\r\n" + 
				"    ON budgetdept.pk_budgetdept =make.pk_budgetdept\r\n" + 
				"LEFT JOIN bd_project project\r\n" + 
				"    ON detail.pk_project =project.pk_project\r\n" + 
				"LEFT JOIN pm_eps projectclass\r\n" + 
				"    ON project.pk_eps = projectclass.pk_eps\r\n" + 
				"LEFT JOIN bd_project f2project\r\n" + 
				"    ON f2project.pk_project =project.pk_parentpro\r\n" + 
				"LEFT JOIN bd_project f3project\r\n" + 
				"    ON f3project.pk_project =f2project.pk_parentpro\r\n" + 
				"LEFT JOIN bd_inoutbusiclass inoutbusiclass\r\n" + 
				"    ON inoutbusiclass.pk_inoutbusiclass=detail.pk_inoutclass\r\n" +
				"LEFT JOIN bd_fundsource  fundsource \r\n" + 
				"    ON fundsource.pk_fundsource=detail.pk_fundsource\r\n" + 
				"WHERE (make.pk_org = '"+pk_org+"'\r\n" + 
				"        AND make.nyear = '"+nyear+"' )\r\n" + 
				"        AND ( make.transtype = 'BM20-Cxx-CD-YS02')";
		
		if(pk_inoutclass!=null) {
			sql += "and pk_inoutclass = '"+pk_inoutclass+"'";
		}
		
		
		
		List<Map<String,Object>> list = (ArrayList<Map<String,Object>>)getBaseDAO().executeQuery(sql, new MapListProcessor());
		//initFundsource(list);
		List<Map<String,Object>> newList = new ArrayList();
		initFundsource(list);
		List<Map<String,Object>> listToCopyInto = new ArrayList<>();  
//		Collections.copy( list,listToCopyInto);
//		Collections.copyList(list,listToCopyInto); 
		listToCopyInto = ObjectUtils.clone(list);
	
		
		
		List list1 = initnewList(initList(list,newList));
		//List<Map<String,Object>> List2= initnewList(list1);
		
		
		
		listToCopyInto.addAll(list1);
		
		
		
		listToCopyInto.sort(Comparator.comparing(map -> (String) map.get("inoutbusiclasscode")));
	//	assignLevels(listToCopyInto);
		
		if(inoutlevel!="0"&&inoutlevel!=null) {
			
	        List<Map<String, Object>> originalList = new ArrayList<>();  
	        
	        // 使用流来过滤level为1的对象，并将结果添加到listToCopyInto中  
	        // 注意：这里我们直接赋值给listToCopyInto，这意味着它之前的所有内容将被过滤后的内容替换  
	        // 如果你想要保留原有内容并添加新内容，应该使用addAll而不是赋值  
	        originalList = listToCopyInto.stream()  
	                .filter(map -> map.containsKey("classlevel") && ( map.get("classlevel")).equals(inoutlevel))  
	                .collect(Collectors.toList());  
	        return originalList;
			
		}
		return listToCopyInto;       
    }
 
    private MetaData initMetaData() {
        MetaData md = new MetaData();
        for(int i = 0 ; i < fields.length ; i++){
            md.addField(initStringMetaData(fields[i]));
        }
        return md;
    }
 
    private Field initStringMetaData(String fieldName) {
        Field filed = new Field();
        filed.setCaption("默认caption");
        filed.setDbColumnType(Types.VARCHAR);
        filed.setFldname(fieldName);
        filed.setPrecision(250);
        return filed;
    }
    //查询结果根据当前支出科目的父类分类 
    private void initFundsource(List<Map<String, Object>> list) {
    	
    	//根据资金来源
    	for(Map map : list) {

    		map.put("oneup_01",new Double(0));
    		map.put("onedown_01",new Double(0));
    		map.put("twoup_01",new Double(0));
    		map.put("twodown_01",new Double(0));
    		map.put("oneup_02",new Double(0));
    		map.put("onedown_02",new Double(0));
    		map.put("twoup_02",new Double(0));
    		map.put("twodown_02",new Double(0));
    		map.put("oneup_03",new Double(0));
    		map.put("onedown_03",new Double(0));
    		map.put("twoup_03",new Double(0));
    		map.put("twodown_03",new Double(0));
    		map.put("oneup_04",new Double(0));
    		map.put("onedown_04",new Double(0));
    		map.put("twoup_04",new Double(0));
    		map.put("twodown_04",new Double(0));
    		map.put("oneup_05",new Double(0));
    		map.put("onedown_05",new Double(0));
    		map.put("twoup_05",new Double(0));
    		map.put("twodown_05",new Double(0));
    		String sourcecode= map.get("sourcecode").toString();
    		map.put("oneup_"+sourcecode, map.get("amount_firstup")==null?0:map.get("amount_firstup"));
    		map.put("onedown_"+sourcecode,map.get("amount_firstdown")==null?0:map.get("amount_firstdown"));
    		map.put("twoup_"+sourcecode,map.get("amount_secondup")==null?0:map.get("amount_secondup"));
    		map.put("twodown_"+sourcecode,map.get("amount_seconddown")==null?0:map.get("amount_secondup"));
    		//计算合计
    		map.put("amount_oneup", map.get("amount_firstup")==null?0:map.get("amount_firstup"));
    		map.put("amount_onedown",map.get("amount_firstdown")==null?0:map.get("amount_firstdown"));
    		map.put("amount_twoup",map.get("amount_secondup")==null?0:map.get("amount_secondup"));
    		map.put("amount_twodown",map.get("amount_seconddown")==null?0:map.get("amount_secondup"));
    		
    		
		
    	}

    	
    }
    //递归查上级并计算
    
    private List<Map<String,Object>> initList(List<Map<String,Object>> list,List<Map<String,Object>> newlist) throws DAOException {
    	
    		//获取上级
    		
    		 //根据上级分组
    	        Map<String, List<Map<String, Object>>> grouped = list.stream()
    	                .collect(Collectors.groupingBy(
    	                        map -> map.get("inoutbusiclassparent")==null?"top":(String)map.get("inoutbusiclassparent"), // 分组键
    	                        Collectors.toList() // 收集器，这里简单地将元素放入列表中
    	                ));
    	     
    	                List getList = new ArrayList();
    	        
    	        
    	        for (Object key : grouped.keySet()) {
    	        	if(!"top".equals(key)) {
    	        	
    	        	List<Map<String,Object>> groupedList =grouped.get(key);
    	        	Map<String,Object> newMap = new HashMap<String,Object>();
    	   //     	 newMap = groupedList.get(0);
    	        	//计算总值
    	        	Double sum = 0.00;
    	        	Double oneup_01Sum = 0.00, oneup_02Sum = 0.00, oneup_03Sum = 0.00, oneup_04Sum = 0.00, oneup_05Sum = 0.00;
    	        	Double twoup_01Sum = 0.00, twoup_02Sum = 0.00, twoup_03Sum = 0.00, twoup_04Sum = 0.00, twoup_05Sum = 0.00;
    	        	Double onedown_01Sum = 0.00, onedown_02Sum = 0.00, onedown_03Sum = 0.00, onedown_04Sum = 0.00, onedown_05Sum = 0.00;
    	        	Double twodown_01Sum = 0.00, twodown_02Sum = 0.00, twodown_03Sum = 0.00, twodown_04Sum = 0.00, twodown_05Sum = 0.00;
    	        	//获取上级编码
    	        	String sql = "select code,pk_parent inoutbusiclassparent,name inoutbusiclassname,pk_inoutbusiclass,def1 classlevel  from bd_inoutbusiclass where pk_inoutbusiclass='"+key.toString()+"'";
	        		Map<String,Object> fatherMap = (Map<String,Object>)getBaseDAO().executeQuery(sql, new MapProcessor());
    	        	for(Map map: groupedList) {
    	        		
    	        		
    	        		
    	        		
    		
    	        		//计算列和
    	        		 if ( map.get("oneup_01") instanceof Number) {
    	                     oneup_01Sum += ((Number) map.get("oneup_01")).doubleValue();
    	                 }
    	        		 if ( map.get("oneup_02") instanceof Number) {
    	        			 oneup_02Sum += ((Number) map.get("oneup_02")).doubleValue();
    	                 }
    	        		 if ( map.get("oneup_03") instanceof Number) {
    	                     oneup_03Sum += ((Number) map.get("oneup_03")).doubleValue();
    	                 }
    	        		 if ( map.get("oneup_04") instanceof Number) {
    	                     oneup_04Sum += ((Number) map.get("oneup_04")).doubleValue();
    	                 }
    	        		 if ( map.get("oneup_05") instanceof Number) {
    	                     oneup_05Sum += ((Number) map.get("oneup_05")).doubleValue();
    	                 }
    	        		 if ( map.get("twoup_01") instanceof Number) {
    	        			 twoup_01Sum += ((Number) map.get("twoup_01")).doubleValue();
    	                 }
    	        		 if ( map.get("twoup_02") instanceof Number) {
    	        			 twoup_02Sum += ((Number) map.get("twoup_02")).doubleValue();
    	                 }
    	        		 if ( map.get("twoup_03") instanceof Number) {
    	        			 twoup_03Sum += ((Number) map.get("twoup_03")).doubleValue();
    	                 }
    	        		 if ( map.get("twoup_04") instanceof Number) {
    	        			 twoup_04Sum += ((Number) map.get("twoup_04")).doubleValue();
    	                 }
    	        		 if ( map.get("twoup_05") instanceof Number) {
    	        			 twoup_05Sum += ((Number) map.get("twoup_05")).doubleValue();
    	                 }
    	        		 if ( map.get("onedown_01") instanceof Number) {
    	        			 onedown_01Sum += ((Number) map.get("onedown_01")).doubleValue();
    	                 }
    	        		 if ( map.get("onedown_02") instanceof Number) {
    	        			 onedown_02Sum += ((Number) map.get("onedown_02")).doubleValue();
    	                 }
    	        		 if ( map.get("onedown_03") instanceof Number) {
    	        			 onedown_03Sum += ((Number) map.get("onedown_03")).doubleValue();
    	                 }
    	        		 if ( map.get("onedown_04") instanceof Number) {
    	        			 onedown_04Sum += ((Number) map.get("onedown_04")).doubleValue();
    	                 }
    	        		 if ( map.get("onedown_05") instanceof Number) {
    	        			 onedown_05Sum += ((Number) map.get("onedown_05")).doubleValue();
    	                 }
    	        		 if ( map.get("twodown_01") instanceof Number) {
    	        			 twodown_01Sum += ((Number) map.get("twodown_01")).doubleValue();
    	                 }
    	        		 if ( map.get("twodown_02") instanceof Number) {
    	        			 twodown_02Sum += ((Number) map.get("twodown_02")).doubleValue();
    	                 }
    	        		 if ( map.get("twodown_03") instanceof Number) {
    	        			 twodown_03Sum += ((Number) map.get("twodown_03")).doubleValue();
    	                 }
    	        		 if ( map.get("twodown_04") instanceof Number) {
    	        			 twodown_04Sum += ((Number) map.get("twodown_04")).doubleValue();
    	                 }
    	        		 if ( map.get("twodown_05") instanceof Number) {
    	        			 twodown_05Sum += ((Number) map.get("twodown_05")).doubleValue();
    	                 }
    	        		  
    	        		
    	        	}
    	        	newMap.put("oneup_01",oneup_01Sum);
    	        	newMap.put("onedown_01",onedown_01Sum);
    	        	newMap.put("twoup_01",twoup_01Sum);
    	        	newMap.put("twodown_01",twodown_01Sum);
    	        	newMap.put("oneup_02",oneup_02Sum);
    	        	newMap.put("onedown_02",onedown_02Sum);
    	        	newMap.put("twoup_02",twoup_02Sum);
    	        	newMap.put("twodown_02",twodown_02Sum);
    	        	newMap.put("oneup_03",oneup_03Sum);
    	        	newMap.put("onedown_03",onedown_03Sum);
    	        	newMap.put("twoup_03",twoup_03Sum);
    	        	newMap.put("twodown_03",twodown_03Sum);
    	        	newMap.put("oneup_04",oneup_04Sum);
    	        	newMap.put("onedown_04",onedown_04Sum);
    	        	newMap.put("twoup_04",twoup_04Sum);
    	        	newMap.put("twodown_04",twodown_04Sum);
    	        	newMap.put("oneup_05",oneup_05Sum);
    	        	newMap.put("onedown_05",onedown_05Sum);
    	        	newMap.put("twoup_05",twoup_05Sum);
    	        	newMap.put("twodown_05",twodown_05Sum);
    	        	if(fatherMap!=null) {
    	        	newMap.put("inoutbusiclassname",fatherMap.get("inoutbusiclassname"));
    	        	newMap.put("inoutbusiclasscode",fatherMap.get("code"));
    	        	newMap.put("inoutbusiclassparent",fatherMap.get("inoutbusiclassparent"));
    	        	newMap.put("pk_inoutclass",fatherMap.get("pk_inoutbusiclass"));
    	        	newMap.put("classlevel",fatherMap.get("classlevel"));
    	        	}
    	        	//计算合计
    	        	newMap.put("amount_oneup",oneup_01Sum+oneup_02Sum+oneup_03Sum+oneup_04Sum+oneup_05Sum);
    	        	newMap.put("amount_onedown",onedown_01Sum+onedown_02Sum+onedown_03Sum+onedown_04Sum+onedown_05Sum);
    	        	newMap.put("amount_twoup",twoup_01Sum+twoup_02Sum+twoup_03Sum+twoup_04Sum+twoup_05Sum);
    	        	newMap.put("amount_twodown",twodown_01Sum+twodown_02Sum+twodown_03Sum+twodown_04Sum+twodown_05Sum);
    	        	
    	        	
    	          //  list.add(newMap); 
    	            
    	        	newlist.add(newMap);
    	        	getList.add(newMap);
    	            }
}
    	        
    	        list.addAll(getList);
    	        
    	     
    	        if(!getList.isEmpty()) {
    	        initList(getList,newlist);
    	        
    	        
    	        }
    	        
				return newlist;	
    	        	
    	        	
    	            
    	        }
    	       
    	        
    	        
    public static void assignLevels(List<Map<String, Object>> list) {  
        // 创建一个Map来存储已经计算过级别的条目的级别  
        Map<Object, Integer> levelMap = new HashMap<>();  
  
        // 初始化时，假设所有顶级条目的父级ID为null或空字符串，级别为1  
        for (Map<String, Object> map : list) {  
            Object parentId = map.get("inoutbusiclassparent");  
            if (parentId == null || parentId.toString().isEmpty()) {  
                map.put("level", 1);  
                levelMap.put(map.get("pk_inoutclass"), 1);  
            }  
        }  
  
        // 使用队列进行广度优先搜索（BFS），计算非顶级条目的级别  
        Queue<Map<String, Object>> queue = new LinkedList<>();  
        // 将所有顶级条目加入队列  
        for (Map<String, Object> map : list) {  
            if (map.containsKey("level")) { // 只加入已经确定为顶级的条目  
                queue.offer(map);  
            }  
        }  
  
        while (!queue.isEmpty()) {  
            Map<String, Object> current = queue.poll();  
            Object currentId = current.get("pk_inoutclass");  
            int currentLevel = levelMap.get(currentId);  
  
            // 遍历当前条目的所有子条目（这里假设是单向关系，实际上需要额外的数据结构或逻辑来维护）  
            // 但在这个例子中，我们直接在原list中查找  
            for (Map<String, Object> child : list) {  
                Object childParentId = child.get("inoutbusiclassparent");  
                if (childParentId != null && childParentId.equals(currentId)) {  
                    // 如果找到子条目，计算其级别并加入队列  
                    int childLevel = currentLevel + 1;  
                    child.put("level", childLevel);  
                    levelMap.put(child.get("pk_inoutclass"), childLevel);  
                    queue.offer(child);  
                }  
            }  
        }  
    }     
    		 
    		
    		
    		
    private List<Map<String,Object>> initnewList(List<Map<String,Object>> list){
    	 List<Map<String, Object>> returnList =  new ArrayList();
        Map<String, List<Map<String, Object>>> grouped = list.stream()
                .collect(Collectors.groupingBy(
                        map -> map.get("pk_inoutclass").toString(), // 分组键
                        Collectors.toList() // 收集器，这里简单地将元素放入列表中
                ));
        for (Object key : grouped.keySet()) {
        	
        	List<Map<String, Object>> groupedList= grouped.get(key);
        	//key相同的项进行合并
        	Double sum = 0.00;
        	Double oneup_01Sum = 0.00, oneup_02Sum = 0.00, oneup_03Sum = 0.00, oneup_04Sum = 0.00, oneup_05Sum = 0.00;
        	Double twoup_01Sum = 0.00, twoup_02Sum = 0.00, twoup_03Sum = 0.00, twoup_04Sum = 0.00, twoup_05Sum = 0.00;
        	Double onedown_01Sum = 0.00, onedown_02Sum = 0.00, onedown_03Sum = 0.00, onedown_04Sum = 0.00, onedown_05Sum = 0.00;
        	Double twodown_01Sum = 0.00, twodown_02Sum = 0.00, twodown_03Sum = 0.00, twodown_04Sum = 0.00, twodown_05Sum = 0.00;
        	
        	for(Map map:groupedList) {
        		
        		
        		
        		
        		
        		//计算列和
        		 if ( map.get("oneup_01") instanceof Number) {
                     oneup_01Sum += ((Number) map.get("oneup_01")).doubleValue();
                 }
        		 if ( map.get("oneup_02") instanceof Number) {
        			 oneup_02Sum += ((Number) map.get("oneup_02")).doubleValue();
                 }
        		 if ( map.get("oneup_03") instanceof Number) {
                     oneup_03Sum += ((Number) map.get("oneup_03")).doubleValue();
                 }
        		 if ( map.get("oneup_04") instanceof Number) {
                     oneup_04Sum += ((Number) map.get("oneup_04")).doubleValue();
                 }
        		 if ( map.get("oneup_05") instanceof Number) {
                     oneup_05Sum += ((Number) map.get("oneup_05")).doubleValue();
                 }
        		 if ( map.get("twoup_01") instanceof Number) {
        			 twoup_01Sum += ((Number) map.get("twoup_01")).doubleValue();
                 }
        		 if ( map.get("twoup_02") instanceof Number) {
        			 twoup_02Sum += ((Number) map.get("twoup_02")).doubleValue();
                 }
        		 if ( map.get("twoup_03") instanceof Number) {
        			 twoup_03Sum += ((Number) map.get("twoup_03")).doubleValue();
                 }
        		 if ( map.get("twoup_04") instanceof Number) {
        			 twoup_04Sum += ((Number) map.get("twoup_04")).doubleValue();
                 }
        		 if ( map.get("twoup_05") instanceof Number) {
        			 twoup_05Sum += ((Number) map.get("twoup_05")).doubleValue();
                 }
        		 if ( map.get("onedown_01") instanceof Number) {
        			 onedown_01Sum += ((Number) map.get("onedown_01")).doubleValue();
                 }
        		 if ( map.get("onedown_02") instanceof Number) {
        			 onedown_02Sum += ((Number) map.get("onedown_02")).doubleValue();
                 }
        		 if ( map.get("onedown_03") instanceof Number) {
        			 onedown_03Sum += ((Number) map.get("onedown_03")).doubleValue();
                 }
        		 if ( map.get("onedown_04") instanceof Number) {
        			 onedown_04Sum += ((Number) map.get("onedown_04")).doubleValue();
                 }
        		 if ( map.get("onedown_05") instanceof Number) {
        			 onedown_05Sum += ((Number) map.get("onedown_05")).doubleValue();
                 }
        		 if ( map.get("twodown_01") instanceof Number) {
        			 twodown_01Sum += ((Number) map.get("twodown_01")).doubleValue();
                 }
        		 if ( map.get("twodown_02") instanceof Number) {
        			 twodown_02Sum += ((Number) map.get("twodown_02")).doubleValue();
                 }
        		 if ( map.get("twodown_03") instanceof Number) {
        			 twodown_03Sum += ((Number) map.get("twodown_03")).doubleValue();
                 }
        		 if ( map.get("twodown_04") instanceof Number) {
        			 twodown_04Sum += ((Number) map.get("twodown_04")).doubleValue();
                 }
        		 if ( map.get("twodown_05") instanceof Number) {
        			 twodown_05Sum += ((Number) map.get("twodown_05")).doubleValue();
                 }
        		  
        		
        	}
        	Map<String,Object> newMap = new HashMap();
        	newMap.put("oneup_01",oneup_01Sum);
        	newMap.put("onedown_01",onedown_01Sum);
        	newMap.put("twoup_01",twoup_01Sum);
        	newMap.put("twodown_01",twodown_01Sum);
        	newMap.put("oneup_02",oneup_02Sum);
        	newMap.put("onedown_02",onedown_02Sum);
        	newMap.put("twoup_02",twoup_02Sum);
        	newMap.put("twodown_02",twodown_02Sum);
        	newMap.put("oneup_03",oneup_03Sum);
        	newMap.put("onedown_03",onedown_03Sum);
        	newMap.put("twoup_03",twoup_03Sum);
        	newMap.put("twodown_03",twodown_03Sum);
        	newMap.put("oneup_04",oneup_04Sum);
        	newMap.put("onedown_04",onedown_04Sum);
        	newMap.put("twoup_04",twoup_04Sum);
        	newMap.put("twodown_04",twodown_04Sum);
        	newMap.put("oneup_05",oneup_05Sum);
        	newMap.put("onedown_05",onedown_05Sum);
        	newMap.put("twoup_05",twoup_05Sum);
        	newMap.put("twodown_05",twodown_05Sum);
        	
        	newMap.put("inoutbusiclassname",groupedList.get(0).get("inoutbusiclassname"));
        	newMap.put("inoutbusiclasscode",groupedList.get(0).get("inoutbusiclasscode"));
        	newMap.put("inoutbusiclassparent",groupedList.get(0).get("inoutbusiclassparent"));
        	newMap.put("pk_inoutclass",groupedList.get(0).get("pk_inoutbusiclass"));
        	newMap.put("classlevel",groupedList.get(0).get("classlevel"));
        	
        	//计算合计
        	newMap.put("amount_oneup",oneup_01Sum+oneup_02Sum+oneup_03Sum+oneup_04Sum+oneup_05Sum);
        	newMap.put("amount_onedown",onedown_01Sum+onedown_02Sum+onedown_03Sum+onedown_04Sum+onedown_05Sum);
        	newMap.put("amount_twoup",twoup_01Sum+twoup_02Sum+twoup_03Sum+twoup_04Sum+twoup_05Sum);
        	newMap.put("amount_twodown",twodown_01Sum+twodown_02Sum+twodown_03Sum+twodown_04Sum+twodown_05Sum);
        	
        	returnList.add(newMap);
          //  list.add(newMap); 
            
      
        	
        	
        	
        	
        	
        }
        

    	
    	
		return returnList;
    	
    }

}
